package com.atos.smp.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@org.hibernate.annotations.Proxy(lazy=false)
public class Project implements Serializable {
	
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	private int id;
	private String name;

	@ManyToMany(fetch=FetchType.EAGER)
	private List<Collaborator> collaborators;

	@OneToMany
	private List<Tool> tools;
	
	public Project() {
		collaborators = new ArrayList<Collaborator>();
		tools = new ArrayList<Tool>();
	}
	
	public Project(String name, List<Tool> tools) {
		collaborators = new ArrayList<Collaborator>();
		this.name = name;
		this.tools = tools;
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public List<Collaborator> getCollaborators() {
		return collaborators;
	}

	public void setCollaborators(List<Collaborator> collaborators) {
		this.collaborators = collaborators;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public boolean addCollaborator(Collaborator arg0) {
		return collaborators.add(arg0);
	}


	public boolean containsCollaborator(Object arg0) {
		return collaborators.contains(arg0);
	}

	public boolean removeCollaborator(Object arg0) {
		return collaborators.remove(arg0);
	}

	public boolean removeAllCollaborators(Collection<?> arg0) {
		return collaborators.removeAll(arg0);
	}
	
	public void setTools(List<Tool> tools) {
		this.tools = tools;
	}
	public boolean addTool(Tool e) {
		return tools.add(e);
	}

	public boolean addAllTools(Collection<? extends Tool> c) {
		return tools.addAll(c);
	}

	public boolean containsTools(Object o) {
		return tools.contains(o);
	}

	public boolean removeTool(Object o) {
		return tools.remove(o);
	}

	public boolean removeAllTools(Collection<?> c) {
		return tools.removeAll(c);
	}

	public int toolsCount() {
		return tools.size();
	}
	public int collaboratorsCount(){
		return collaborators.size();
	}
	
	public List<Tool> getTools() {
		return tools;
	}

	@Override
	public String toString() {
		String team = "";
		for (Collaborator col : collaborators) {
			team +="" +col.getRule().name() +" : "+ col.getFirstName() +",";
		}
		return "Project [" + name + ", tools=" + tools
				 +", team = "+ team + "]";
	}

	public Collaborator getManager() {
		for (Collaborator col : getCollaborators()) {
			if(col.getRule().equals(Rule.Manager))
				return col;
		}
		return null;
	}
	public void setManager(Collaborator manager) {
		for (int i = 0; i < collaborators.size(); i++) {
			Collaborator c = collaborators.get(i);
			if(c.getRule().equals(Rule.Manager)){
				collaborators.set(i, manager);
				return;
			}
		}
		collaborators.add(manager);
	}
	public void setProjectManager(Collaborator pmanager) {
		for (int i = 0; i < collaborators.size(); i++) {
			Collaborator c = collaborators.get(i);
			if(c.getRule().equals(Rule.Project_Manager)){
				collaborators.set(i, pmanager);
				return;
			}
		}
		collaborators.add(pmanager);
	}
	
	public Collaborator getProjectManager() {
		for (Collaborator col : getCollaborators()) {
			if(col.getRule().equals(Rule.Project_Manager))
				return col;
		}
		return null;
	}
	
	public List<Collaborator> getTeam() {
		List<Collaborator> list = new ArrayList<Collaborator>();
		for (Collaborator col : getCollaborators()) {
			if(col.getRule().equals(Rule.Collaborator))
				list.add(col);
		}
		return list;
	}
	public boolean equals(Object ob) {
		boolean equal =super.equals(ob);
		if(this.id ==((Project)ob).id && this.name ==((Project)ob).name)
			equal = true;
		return equal;
	}

	public void setTeam(List<Collaborator> target) {
		if(collaborators.size() >0 ){
		  for (Collaborator col : collaborators) {
			if(col.getRule().equals(Rule.Collaborator)){
				collaborators.remove(col);
				System.out.println("Team remove"+col);
			}
		}
		}
		collaborators.addAll(target);
		System.out.println("Target :"+target);
	}

}
